package com.bysc.cmsservice.controller;

import com.bysc.commonutils.utils.EnumUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.cmsservice.entity.CmsFeedback;
import com.bysc.cmsservice.entity.vo.CmsFeedBackQueryVo;
import com.bysc.cmsservice.service.CmsFeedbackService;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import com.bysc.commonutils.enums.FeedBackTypeEnum;
import com.bysc.commonutils.enums.ClientEnum;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 反馈表 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-10-15
 */
@RestController
@RequestMapping("/cmsservice/cms-feedback")
@Api(tags = "用户反馈")
@Slf4j
public class CmsFeedbackController {

    @Autowired
    private CmsFeedbackService cmsFeedbackService;

    @ApiOperation("添加用户反馈")
    @PostMapping("addFeedBack")
    public Results addFeedBack(@RequestBody CmsFeedback cmsFeedback, HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        cmsFeedback.setUserId(id);


//        //获取反馈的客户端信息
//        ClientEnum clientEnum=ClientEnum.getById(cmsFeedback.getClientId());
//        cmsFeedback.setClientName(clientEnum.getValue());
//        //获取反馈的类型
//        FeedBackTypeEnum feedBackTypeEnum=FeedBackTypeEnum.getById(cmsFeedback.getTypeId());
//        cmsFeedback.setTypeName(feedBackTypeEnum.getValue());

        cmsFeedbackService.save(cmsFeedback);
        return Results.ok().data("feedback",cmsFeedback).message(MessageConstant.ADD_FEEDBACK_SUCCESS);
    }

    @ApiOperation("更新用户反馈")
    @PostMapping("updateFeedBack")
    public Results updateFeedBack(@RequestBody CmsFeedback cmsFeedback){
        cmsFeedbackService.updateById(cmsFeedback);
        return Results.ok().data("updatefeedback",cmsFeedback).message(MessageConstant.UPDATE_FEEDBACK_SUCCESS);
    }


    @ApiOperation("根据id查找用户反馈")
    @GetMapping("queryFeedBackById/{id}")
    public Results queryFeedBackById(@PathVariable String id){
        CmsFeedback feedback = cmsFeedbackService.getById(id);
        feedback.setTypeName(FeedBackTypeEnum.getValueById(feedback.getTypeId()));
        feedback.setClientName(ClientEnum.getValueById(feedback.getClientId()));
        return Results.ok().data("feedback",feedback).message(MessageConstant.QUERY_FEEDBACK_SUCCESS);
    }

    @ApiOperation(value = "可带条件分页查询用户反馈")
    @PostMapping  ("getFeedBackList/{page}/{limit}")
    public Results getFeedBackList(
            @ApiParam(name = "page", value = "查询页数", required = true) @PathVariable Integer page,
            @ApiParam(name = "limit", value = "查询数量", required = true) @PathVariable Integer limit,
            @ApiParam(name = "queryVo", value = "查询条件", required = false) @RequestBody(required = false) CmsFeedBackQueryVo queryVo) {

        Page<CmsFeedback> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<CmsFeedback> wrapper = new LambdaQueryWrapper<CmsFeedback>();

        String id= queryVo.getId();
        String userId= queryVo.getUserId();
        String phone= queryVo.getPhone();
        String name=queryVo.getName();
        String content= queryVo.getContent();
        int clientId= queryVo.getClientId();

        int typeId= queryVo.getTypeId();


        if (!StringUtils.isEmpty(id)) {
            wrapper.eq(CmsFeedback::getId, id);
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq(CmsFeedback::getUserId, userId);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq(CmsFeedback::getPhone, phone);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq(CmsFeedback::getName, name);
        }

        if (clientId!=0) {
            wrapper.eq(CmsFeedback::getClientId, clientId);
        }
        if (typeId!=0) {
            wrapper.eq(CmsFeedback::getTypeId, typeId);
        }
        if (!StringUtils.isEmpty(content)) {
            wrapper.like(CmsFeedback::getContent ,content);
        }

        IPage<CmsFeedback> cmsFeedbackIPage=cmsFeedbackService.page(pageParam,wrapper);
        for (CmsFeedback feedback : cmsFeedbackIPage.getRecords()) {
            feedback.setTypeName(FeedBackTypeEnum.getValueById(feedback.getTypeId()));
            feedback.setClientName(ClientEnum.getValueById(feedback.getClientId()));
        }
        return Results.ok().data("list",cmsFeedbackIPage).message(MessageConstant.QUERY_FEEDBACK_SUCCESS);
    }

    @ApiOperation(value = "根据id删除用户反馈")
    @DeleteMapping ("deleteFeedBackById/{id}")
    public Results deleteFeedBackById(@PathVariable("id") String id){
        cmsFeedbackService.removeById(id);
        return Results.ok().message(MessageConstant.DELETE_FEEDBACK_SUCCESS);
    }

    @ApiOperation("获取反馈表信息")
    @GetMapping("getAllFeedBackInfo")
    public Results getAllFeedBackInfo(){
        List<Map<String, Object>> f = EnumUtils.enumToListMap(FeedBackTypeEnum.class);
        List<Map<String, Object>> c = EnumUtils.enumToListMap(ClientEnum.class);
        return Results.ok().data("feedBackTypeEnums",f).data("clientEnums",c).message(MessageConstant.QUERY_FEEDBACK_SUCCESS);
    }





}

